const res = require("express/lib/response");
const path = require("path");
const db = require("../db/index");
// 添加新文章
exports.addArticle = (req, res) => {
  if (!req.file) return res.cc("文章封面是必选参数!");
  const articleInfo = {
    // 标题、内容、状态、所属的分类Id
    ...req.body,
    cover_img: path.join("/uploads", req.file.filename),
    // 文章发布时间
    pub_date: new Date(),
    // 文章作者的id
    author_id: req.user.id,
  };
  const sql = `select * from ev_articles where title=?`;
  db.query(sql, articleInfo.title, (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 0) {
      return res.cc("文章标题重复了!");
    }
    const sql = `insert into ev_articles set ?`;
    db.query(sql, articleInfo, (err, results) => {
      if (err) return res.cc(err);
      if (results.affectedRows !== 1) {
        return res.cc("发布文章失败!");
      }
      res.cc("发布文章成功!", 0);
    });
  });
};
var data = null;

function aa(sql) {
  return new Promise((resolve) => {
    db.query(sql, data[0].cate_id, (err, results) => {
      if (err) {
        return res.cc(err);
      }
      data[0] = { ...data[0], cate_name: results[0].name };
      console.log(2);
    });
    resolve();
  });
}
// 获取文章的列表数据
exports.getArticle = (req, res) => {
  const pagenum = req.query.pagenum;
  const pagesize = req.query.pagesize;

  if (!req.query.cate_id && !req.query.state) {
    // 查询所有    没有传入cate_id 和state 的情况
    const sql = `select a.id,title,pub_date,state,name as cate_name  from ev_articles a left join ev_article_cate b on a.cate_id = b.id where  a.is_delete=0 `;
    db.query(sql, (err, results) => {
      if (err) {
        return res.cc(err);
      }
      const total = results.length;
      data = results.splice((pagenum - 1) * pagesize, pagesize);
      console.log(data);
      res.send({
        status: 0,
        message: "获取文章列表成功!",
        data,
        total,
      });
    });
  }
  if (req.query.cate_id && !req.query.state) {
    // 传了cate_id 没传 state
    const sql = `select a.id,title,pub_date,state,name as cate_name  from ev_articles a left join ev_article_cate b on a.cate_id = b.id where a.cate_id=?`;
    db.query(sql, req.query.cate_id, (err, results) => {
      if (err) {
        return res.cc(err);
      }
      const total = results.length;
      const data = results.splice((pagenum - 1) * pagesize, pagesize);
      return res.send({
        status: 0,
        message: "获取文章列表成功!",
        data,
        total,
      });
    });
  }
  // 传了 state 没传 cate_id
  if (!req.query.cate_id && req.query.state) {
    const sql = `select a.id,title,pub_date,state,name as cate_name  from ev_articles a left join ev_article_cate b on a.cate_id = b.id where a.state=?`;
    db.query(sql, req.query.state, (err, results) => {
      if (err) {
        return res.cc(err);
      }
      const total = results.length;
      const data = results.splice((pagenum - 1) * pagesize, pagesize);
      return res.send({
        status: 0,
        message: "获取文章列表成功!",
        data,
        total,
      });
    });
  }
  // 传了 state 和 cate_id
  if (req.query.cate_id && req.query.state) {
    // 传了cate_id 没传 state
    const sql = `select a.id,title,pub_date,state,name as cate_name  from ev_articles a left join ev_article_cate b on a.cate_id = b.id where a.cate_id=? and a.state=?`;
    db.query(sql, [req.query.cate_id, req.query.state], (err, results) => {
      if (err) {
        return res.cc(err);
      }
      const total = results.length;
      const data = results.splice((pagenum - 1) * pagesize, pagesize);
      return res.send({
        status: 0,
        message: "获取文章列表成功!",
        data,
        total,
      });
    });
  }
};
// 根据id删除文章
exports.deleteArticle = (req, res) => {
  const sql = `update ev_articles set is_delete=1 where id=?`;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) {
      return res.cc("删除数据失败!");
    }
    res.cc("删除数据成功!", 0);
  });
};

// 根据id获取文章详情
exports.getDetailArticle = (req, res) => {
  const sql = `select * from ev_articles where id=? `;
  db.query(sql, req.params.id, (err, results) => {
    if (err) return res.cc(err);
    if (results.length !== 1) {
      return res.cc("查找不到数据!");
    }
    res.send({ status: 0, message: "获取文章成功!", data: results[0] });
  });
};

// 根据id修改文章详情
exports.editDetailArticle = (req, res) => {
  const sql = `update ev_articles set title=? where id=?`;
  db.query(sql, [req.body.title, req.body.id], (err, results) => {
    if (err) return res.cc(err);
    if (results.affectedRows !== 1) {
      return res.cc("查找不到数据!");
    }
    res.cc("修改文章详情成功!", 0);
  });
};
